package com.valveinspectionsys.rushrepair.activity;

import com.valveinspectionsys.R;
import com.valveinspectionsys.dbhelp.XmlNameHelper;
import com.valveinspectionsys.gridview.ImageSwitcherActivity;
import com.valveinspectionsys.povo.DeviceVo;
import com.valveinspectionsys.povo.RepairVo;
import com.valveinspectionsys.repair.activity.RepairSingleNoRfidActivity;
import com.valveinspectionsys.repair.db.RepairDao;
import com.valveinspectionsys.rushrepair.db.RushRepairDao;
import com.valveinspectionsys.util.SharedPreferenceUtil;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

/**
 * 通过地图生成强修单
 * @author King
 *
 */
public class RushRepairNoRfidActivity extends Activity implements  OnClickListener{
    public TextView textview1;//设备名称
    public TextView textview2;//设备类型
    public TextView textview3;//设备编号
    public TextView textview4;//设备地址
    public TextView textview5;//设备产商
    public TextView textview6;//安装日期
    
    public Button button1;//点击显示
    public Button button2;//读卡
    public Button button3;//保存
    public Button button4;//拍照
    
    public EditText edit1;//更换部件
    public EditText edit2;//备注
    
    public Spinner spinner;//维修形式
    
    public Dialog  dialog;
    
    public RushRepairDao  dao;
    public SharedPreferenceUtil  sp;
    public String[] data=new String[]{"请选择","更换部件","更换设备","拆卸设备"};
    public String[] checkdata=null;
    public boolean[] selected=null;
    public String repairtype="请选择";
    
  
    public String[] copydata=new String[3];//故障类型
    public RepairVo   repair;
    public DeviceVo device;
    public String deviceid;
    public String[] gz=new String[]{"请选择","更换部件","更换设备","拆除设备"};
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
	
	super.onCreate(savedInstanceState);
	setContentView(R.layout.repair_single_rfid);
	deviceid=getIntent().getStringExtra("deviceid");
	sp=new SharedPreferenceUtil(this);
	dao=new RushRepairDao(this, sp.getValue(XmlNameHelper.SqliteName));
	init();
	TextView tx=(TextView)findViewById(R.id.repair_single_rfid_textView1);
	tx.setText("抢修单");
	initData();
	
    }

    public void init(){
	checkdata=new String[]{"环境问题","外观问题","外泄露问题","开关不灵活","内泄露问题","出水问题","压力问题","排气问题"};
	 selected = new boolean[checkdata.length];
	    	for (int i = 0; i < selected.length; i++) {
				selected[i] = false;
			}

	textview1=(TextView)findViewById(R.id.repair_single_rfid_devicename);
	textview2=(TextView)findViewById(R.id.repair_single_rfid_devicetype);
	textview3=(TextView)findViewById(R.id.repair_single_rfid_devicenumber);
	textview4=(TextView)findViewById(R.id.repair_single_rfid_deviceadds);
	textview5=(TextView)findViewById(R.id.repair_single_rfid_producers);
	textview6=(TextView)findViewById(R.id.repair_single_rfid_InstallationDate);
	button1=(Button)findViewById(R.id.repair_single_rfid_clickview);
	button2=(Button)findViewById(R.id.repair_single_rfid_tag);
	button3=(Button)findViewById(R.id.repair_single_rfid_save);
	button4=(Button)findViewById(R.id.repair_single_rfid_paizhao);
	button1.setOnClickListener(this);
	button2.setOnClickListener(this);
	button3.setOnClickListener(this);
	button4.setOnClickListener(this);
	edit1=(EditText)findViewById(R.id.repair_single_rfid_reParts);
	edit2=(EditText)findViewById(R.id.repair_single_rfid_remark);
	spinner=(Spinner)findViewById(R.id.repair_single_rfid_repairtype);
	   ArrayAdapter<String>    adapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data);
	    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);//设置打开方式
	    spinner.setAdapter(adapter);
	      spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

		@Override
		public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
		   repairtype=data[position];
		    
		}

		@Override
		public void onNothingSelected(AdapterView<?> parent) {
		    // TODO Auto-generated method stub
		    
		}
	    });
	
    }
    
    
    public void initData(){
	device=dao.queryRfidDevice(null, deviceid, false);
	repair=dao.queryRepair(deviceid);
	if(repair==null){
	  
	    edit1.setText("");
	    edit2.setText("");
	    spinner.setSelection(0, true);
	}else{
	    edit1.setText(repair.getReParts());
	    edit2.setText(repair.getRemark());
	    if(!TextUtils.isEmpty(repair.getRepairmethod())){
		for(int i=0;i<gz.length;i++){
		    if(repair.getRepairmethod().equals(gz[i])){
			spinner.setSelection(i, true);
		    }
		}
		
	    }else{
		spinner.setSelection(0, true);
	    }
	    
	    faultboolean(repair.getFaultType());
	}
	if(device==null){
	    textview1.setText("");
	    textview2.setText("");
	    textview3.setText("");
	    textview4.setText("");
	    textview5.setText("");
	    textview6.setText("");
	}else{
	    textview1.setText(device.getUseStatus());
	    textview2.setText(device.getDeviceType());
	    textview3.setText(device.getDeviceNumber());
	    textview4.setText(device.getDeviceAdds());
	    textview5.setText(device.getProducers());
	    textview6.setText(device.getInstallationDate());
	}
    }
    
    @Override
    public void onClick(View v) {
	 switch (v.getId()) {
	case R.id.repair_single_rfid_clickview:
	       initDialog();
	    break;
	
	case R.id.repair_single_rfid_paizhao:
	   Intent   intent=new Intent(RushRepairNoRfidActivity.this, ImageSwitcherActivity.class);
	   intent.putExtra("otherid", repair.getRepairid());
	   intent.putExtra("devicelistid", "-1");
	   intent.putExtra("status", "4");
	   startActivityForResult(intent, 1);
	    
	    break;
	case R.id.repair_single_rfid_save:
	    if(repairtype.equals("请选择")){
		Toast.makeText(RushRepairNoRfidActivity.this, "请选择维修方式", Toast.LENGTH_SHORT).show();
		return;
	    }
	    if(!isSelectFault()){
		Toast.makeText(RushRepairNoRfidActivity.this, "请选择故障类型", Toast.LENGTH_SHORT).show();
		return;
	    }
	    if(TextUtils.isEmpty(edit1.getEditableText().toString())){
		Toast.makeText(RushRepairNoRfidActivity.this, "请填写更换设备或更新部件", Toast.LENGTH_SHORT).show();
		return;
	    }
	 boolean b=   dao.insertDeviceData(repair.getRepairid(), repairtype, faultStr(selected), edit1.getEditableText().toString(),
		    edit2.getEditableText().toString(), "1");
	 if(b){
	     Toast.makeText(RushRepairNoRfidActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
	 }else{
	     Toast.makeText(RushRepairNoRfidActivity.this, "保存失败", Toast.LENGTH_SHORT).show();
	 }
	    break;
	default:
	    break;
	}
	
    }


 
   
    public void initDialog(){
	
	AlertDialog.Builder builder = new AlertDialog.Builder(this);
    
    	builder.setTitle("故障类型");
    	builder.setMultiChoiceItems(checkdata, selected, new OnMultiChoiceClickListener() {
	    
	    @Override
	    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
		selected[which]=isChecked;
		
	    }
	});
    	builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
	    
	    @Override
	    public void onClick(DialogInterface dialog, int which) {
		
		
	    }
	});
    	builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
	    
	    @Override
	    public void onClick(DialogInterface dialog, int which) {
		
		
	    }
	});
      dialog=builder.create();
      dialog.show();

    }
    
    /**
     * 得到故障类型String
     * @param bo
     * @return
     */
    public String faultStr(boolean[] bo){
	String str="";
	for(int i=0;i<bo.length;i++){
	    if(bo[i]){
		str=str+"1,";
	    }else{
		str=str+"0,";
	    }
	}
	return str.substring(0, str.length()-1);
	
    }
    
    /**
     * 将故障类型String转为每一个选项boolean值
     * @param info
     * @return
     */
    public boolean[]  faultboolean(String info){
	  if(!TextUtils.isEmpty(info)&&info.indexOf(",")>0){
	      String[] str=info.split(",");
	      for(int i=0;i<str.length;i++){
		  if(str[i].equals("1")){
		      selected[i]=true;
		  }
	      }
	  }
	
	return selected;
	
    }
    
    /**
     * 判断是否选择了故障类型
     * @return
     */
    public boolean isSelectFault(){
	  boolean b=false;
	 
	  for(int i=0;i<selected.length;i++){
	      if(selected[i]){
		  return true;
	      }
	  }
	return b;
	  
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	  if(requestCode==1){
	      if(resultCode==RESULT_OK){
		  dao.modifyCopystatus(repair.getRepairid(), "1");
	      }else if(requestCode==RESULT_CANCELED){
		  dao.modifyCopystatus(repair.getRepairid(), "0");
	      }
	  }
	super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
	dao.close();
	super.onDestroy();
    }

  
    
}
